package org.chenwang.projects.ehcacheaws;

import java.util.Properties;

import net.sf.ehcache.bootstrap.BootstrapCacheLoader;
import net.sf.ehcache.bootstrap.BootstrapCacheLoaderFactory;
import net.sf.ehcache.util.PropertyUtil;

public class AmazonS3BootstrapCacheLoaderFactory extends
		BootstrapCacheLoaderFactory {

	public static final boolean VALUE_DEFAULT_ASYNCHRONOUS = false;
	
	public static final int VALUE_DEFAULT_SIZE_OF_THREAD_POOL = 20;
	
	public static final String NAME_ASYNCHRONOUS = "asynchronous";
	
	public static final String NAME_SIZE_OF_THREAD_POOL = "sizeOfThreadPool";
	
	
	@Override
	public BootstrapCacheLoader createBootstrapCacheLoader(Properties properties) {
		
		AmazonS3BootstrapCacheLoader loader = null;
		
		// general configurations for S3.
		AmazonS3Configuration c = AmazonS3Configuration.parse(properties);
		// asynchronous or not.
		boolean a = VALUE_DEFAULT_ASYNCHRONOUS;
		String av = PropertyUtil.extractAndLogProperty(NAME_ASYNCHRONOUS, properties);
		if (av != null) {
			a = PropertyUtil.parseBoolean(av);
		}
		// size of thread pool, if multithreaded is enabled.
		if (c.isMultithreaded()) {
			int sotp = VALUE_DEFAULT_SIZE_OF_THREAD_POOL;
			String sv = PropertyUtil.extractAndLogProperty(NAME_SIZE_OF_THREAD_POOL, properties);
			if (sv != null) {
				sotp = Integer.parseInt(sv, VALUE_DEFAULT_SIZE_OF_THREAD_POOL);
			}
			
			loader = new AmazonS3BootstrapCacheLoaderImplRestSimpleMulti(c.getBucketName(), c.getPathName(), c.getPathSeparator(), c.getCredentials(), a, sotp);
		}
		else {
			loader = new AmazonS3BootstrapCacheLoaderImplRestSimple(c.getBucketName(), c.getPathName(), c.getPathSeparator(), c.getCredentials(), a);
		}
		
		return loader;
	}

}
